//
//  MYMainViewController.swift
//  MYWeibo
//
//  Created by Magic Yu on 16/4/13.
//  Copyright © 2016年 Magic Yu. All rights reserved.
//

import UIKit

class MYMainViewController: UITabBarController {

    //reason: '-[MYWeibo.MYMainViewController plusBtnClick]: unrecognized selector sent to instance 0x7f80e0d2bbb0'
    //按钮点击时事件是由运行循环监听并且以消息机制传递的
    //swift是静态语言 当方法前加上private后 对运行循环不可见
    //加上@objc修饰后 在于告诉系统该方法使用oc的基于运行时的机制(KVC及动态派发)
    @objc private func plusBtnClick() {
        print("被点击了,~~~~(>_<)~~~~")
        //添加到window上
        let composeView = MYComposeView()
        composeView.show(self)
        //UIApplication.sharedApplication().keyWindow?.addSubview(composeView)
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //添加子视图
        addChildViewControllers()
        
        let mainTabBar = MYTabBar()
        
        //按钮添加点击事件 MYTabBar不能完成按钮的点击事件 需要交给控制器 
        //此时按钮是私有属性 只能在本文件中访问 外部文件访问不到 所以需要将private去掉
        
        //给按钮添加点击事件
        mainTabBar.plusBtn.addTarget(self, action: "plusBtnClick", forControlEvents: .TouchUpInside)
        
        //使用自定义的tabBar
        //print(tabBar.classForCoder)
        //tabBar是只读属性 通过KVC赋值
        self.setValue(mainTabBar, forKey: "tabBar")
        //print(tabBar.classForCoder)
    }
    
    private func addChildViewControllers() {
    
        addChildViewController(MYHomeTableViewController(), title: "首页", imageName: "tabbar_home",index: 0)
        addChildViewController(MYMessageTableViewController(), title: "消息", imageName: "tabbar_message_center",index: 1)
        addChildViewController(MYDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover",index: 2)
        addChildViewController(MYProfileTableViewController(), title: "我", imageName: "tabbar_profile",index: 3)
    }
    
    private func addChildViewController(vc: UIViewController, title: String, imageName: String,index: Int) {
        
        //如果要用自定义的tabBarItem 必须写在最上面 否则会将之前的设置覆盖掉
        vc.tabBarItem = MYTabBarItem()

        //设置标签
        vc.title = title
//        vc.tabBarItem.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.tag = baseTag + index
        //设置选中图片
        //设置imageWithRenderingMode 显示图片颜色
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        //设置文字颜色 两种方式
    vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: .Selected)
//        tabBar.tintColor = UIColor.orangeColor()   //自定义tabBar时 颜色设置失效
        //创建导航控制器
        let nav = MYBaseNavViewController(rootViewController: vc)
        //将子控制器添加到标签控制器中
        addChildViewController(nav)
    }
    
    //tabBar的点击代理方法
    override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        //print(item.tag)
        var index = 0
        //使用视图层次查看器 查找子视图
        for subview in tabBar.subviews {
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!) {
            if index == item.tag - baseTag {
                //可以定位到点击的 UITabBarItem
                //print(subview)
                
                //遍历UITabBarButton的子视图
                for sv in subview.subviews {
                    if sv.isKindOfClass(NSClassFromString("UITabBarSwappableImageView")!) {
                    //print(sv)
                        //执行动画效果
                        sv.transform = CGAffineTransformMakeScale(0.5, 0.5)
                        UIView.animateWithDuration(0.25, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 2, options: [], animations: { () -> Void in
                            //先缩小,再放大
                            sv.transform = CGAffineTransformMakeScale(1, 1)
                            }, completion: { (_) -> Void in
                                print("OK")
                        })
                    }
                }
                
            }
            index++
            }
        }
    }

}




